package logic

import (
	"context"
	"errors"
	"github.com/google/uuid"
	"github.com/zeromicro/go-zero/core/logx"
	"house_srv/house"
	"house_srv/internal/svc"
	"house_srv/mysql"
	"house_srv/pkg"
	"strconv"
)

type OrderCreateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderCreateLogic {
	return &OrderCreateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderCreateLogic) OrderCreate(in *house.OrderCreateRequest) (*house.OrderCreateResponse, error) {
	// todo: add your logic here and delete this line
	var h mysql.House
	err := h.HouseInfo(in.HouseId)
	if err != nil {
		logx.Error("获取房源信息不存在")
		return nil, errors.New("获取房源信息不存在")
	}
	orderSn := uuid.NewString()
	var order mysql.Order
	order.UserId = int(in.UserId)
	order.OrderSn = orderSn
	order.PayType = 1
	order.PayTime = 0
	order.Amount = h.Price
	err = order.Create()
	if err != nil {
		logx.Info("创建订单信息失败", err)
		return nil, errors.New("创建订单信息失败")
	}
	//orderSn:=uuid.NewString()
	totalAmount := strconv.FormatInt(order.Amount, 10)
	Alipay := pkg.AlIPay{
		OutTradeNo:  orderSn,
		TotalAmount: totalAmount,
	}

	Urlpay := Alipay.Pay()
	return &house.OrderCreateResponse{
		UrlType: Urlpay,
		OrderId: int64(order.ID),
	}, nil
}
